# h1=. buttons.py

# p=. *Reactive Image Buttons* 
# _jfuller 2008-03-03_

# It can be useful to have image buttons that respond to the user's
# attention (hover, click, etc.). This class makes it easier to set up.

# h2. Front Matter

# h3. Imports

import os, wx

# h3. Image Path

# Although this is rather clumsy, it's easiest (for now) to set the
# default image path relative to this module file. As long as I have
# everything in the proper place, it makes setup much easier. However,
# it does assume that this module is located in a @%lib/@ directory or
# the like off the main program path, so that the @%img/@ directory
# will be a sibling.

path_img = os.path.join(os.path.dirname(__file__), "../img/%s.png")

# h2. Picture Buttons

# Picture buttons use a set of four bitmaps to provide a flashy interactive
# button experience. The bitmaps should all be the same size, png format
# with alpha transparency. The button name is used as a prefix and the
# button state that the bitmap is form makes up the rest of the filename.
# For a help button, we would have:

# |_. Image File |_. Description | 
# | help_disable.png | Use when the control is greyed out. | 
# | help_hover.png | Use when the mouse pointer is over the control. | 
# | help_label.png | The default appearance of the control. | 
# | help_selected.png | Depressed (button-down) appearance of the control. | 

# By default, you're expected to use 'png' format images, however you can
# instead use any format that wxPython supports by passing it with the
# 'kind' option.

# By default, the images should be in an immediate subdirectory called 'img'.
# However, you can override the image location using the 'path' option. Be
# sure that the file extension matches the kind option.

class picture(wx.BitmapButton):
    def __init__(my, parent, id, name, kind=None, path=None):
        "( my parent id name /kind/ /path/ --) Create picture button."
        
        my._name = name
        my._kind = kind
        if not kind: my._kind = wx.BITMAP_TYPE_PNG
        my._path = path
        if not path: my._path = path_img

        wx.BitmapButton.__init__(my, parent, id, style=0)

        for (state, f) in (("disable", my.SetBitmapDisabled),
                           ("hover", my.SetBitmapHover),
                           ("label", my.SetBitmapLabel),
                           ("selected", my.SetBitmapSelected),):

# If stateful button images aren't available, I try to use just the
# button name to find the image.

            p = my._path % ("%s_%s" % (my._name, state))
            if not os.path.exists(p): p = my._path % my._name

            f(wx.Image(p, my._kind).ConvertToBitmap())
            
